package com.itheima.controller;

import com.itheima.utils.MessageConstant;
import com.itheima.pojo.Feedback;
import com.itheima.pojo.User;
import com.itheima.service.FeedbackService;
import com.itheima.service.UserService;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.FeedbackVO;
import com.itheima.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 登录相关接口
 */
@RestController
@RequestMapping("/oauth")
@CrossOrigin
@Api(tags = "登录相关接口")
public class OauthController {
    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private UserService userService;

    @PostMapping("/addFeedback")

    @ApiOperation("提交反馈")
    public String addFeedback(@RequestBody FeedbackVO feedbackVO) {
        try {
            feedbackService.addFeedback(feedbackVO);
            return ResultUtil.result(200, MessageConstant.OAUTH_ADDFEEDBACK_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(200, MessageConstant.OAUTH_ADDFEEDBACK_FAIL);
        }
    }

    //用户退出登录
    @PostMapping("/delete/{accessToken}")

    @ApiOperation("用户退出登录")
    public String delete(@PathVariable("accessToken") String accessToken) {
        try {
            if (StringUtils.isEmpty(accessToken)) {
                return ResultUtil.result(401, MessageConstant.OAUTH_EXIT_FAIL);
            }
            feedbackService.delete(accessToken);
            return ResultUtil.result(200, MessageConstant.OAUTH_EXIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.OAUTH_EXIT_FAIL);
        }
    }

    //修改用户信息
    @PostMapping("/editUser")

    @ApiOperation("修改用户信息")
    public String editUser(@RequestBody UserVO userVO, HttpServletRequest request) {
        try {
            if (com.itheima.utils.StringUtils.isEmpty(String.valueOf(userVO.getBirthday())) || !String.valueOf(userVO.getBirthday()).contains("-")){
                return ResultUtil.result(401, "日期格式请为yyyy-MM-dd");
            }
            userService.edit(userVO, request);
            return ResultUtil.result(200, MessageConstant.OAUTH_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.OAUTH_EDIT_FAIL);
        }
    }

    //获取用户信息
    @GetMapping("/getFeedbackList")

    @ApiOperation("获取用户反馈")
    public String getFeedbackList() {
        try {

            List<Feedback> list = feedbackService.getFeedbackList();
            return ResultUtil.result(200, list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.OAUTH_GETFEEDBACKLIST_FAIL);
        }
    }

    //获取用户信息
    @GetMapping("/verify/{accessToken}")

    @ApiOperation("获取用户信息")
    public String verify(@PathVariable("accessToken") String accessToken) {
        try {
            if (StringUtils.isEmpty(accessToken)) {
                return ResultUtil.result(401, MessageConstant.OAUTH_VERIFY_FAIL);
            }
            User user = userService.verify(accessToken);
            return ResultUtil.result(200, user);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.OAUTH_VERIFY_FAIL);
        }
    }

    @PostMapping("/updateUserPwd")

    @ApiOperation("更新用户密码")
   public String updateUserPwd(String oldPwd, String newPwd, HttpServletRequest request) {
        try {
            userService.updateUserPwd(oldPwd, newPwd, request);
            return ResultUtil.result(200, MessageConstant.OAUTH_UPDATEUSERPED_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.OAUTH_UPDATEUSERPED_FAIL);
        }
    }
}
